package com.uphone.kingmall.activity;

import android.graphics.Color;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.SizeUtils;
import com.china.zhuoyue.R;
import com.lzy.okgo.model.HttpParams;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.uphone.kingmall.adapter.CommentsListAdapter;
import com.uphone.kingmall.base.BaseActivity;
import com.uphone.kingmall.bean.GoodCommentsBean;
import com.uphone.kingmall.listener.NetListCallBack;
import com.uphone.kingmall.utils.CommonUtil;
import com.uphone.kingmall.utils.MyUrl;
import com.uphone.kingmall.utils.RecycleViewDivider;
import com.uphone.kingmall.utils.intent.IntentData;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * author : SuMeng
 * e-mail : 986335838@qq.com
 * time   : 2019/02/26
 * desc   : 全部商品评价
 */
public class GoodsAllCommentsListActivity extends BaseActivity {
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.rv_comment_list)
    RecyclerView rvCommentList;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.tv_empty)
    TextView tvEmpty;
    @IntentData
    private int id = -1;
    @IntentData
    private int num = 0;
    private int page;
    private CommentsListAdapter commentsListAdapter;

    @Override
    public int getLayoutContentId() {
        return R.layout.kingmall_activity_goods_comments;
    }

    @Override
    public void initView() {
        tvTitle.setText("评价（" + num + "）");
        mImmersionBar
                .statusBarColor(R.color.colorWhite)
                .statusBarDarkFont(true, 0.2f)
                .navigationBarColor(R.color.black)
                .fullScreen(false)
                .init();
    }

    List<GoodCommentsBean.DataBean> dataList = new ArrayList<>();

    @Override
    public void initData() {
        commentsListAdapter = new CommentsListAdapter();
        rvCommentList.setAdapter(commentsListAdapter);
        rvCommentList.addItemDecoration(new RecycleViewDivider(this,
                LinearLayoutManager.HORIZONTAL, SizeUtils.dp2px(10f), Color.parseColor("#eeeeee")));
        rvCommentList.setLayoutManager(new LinearLayoutManager(GoodsAllCommentsListActivity.this));
        rvCommentList.addItemDecoration(new RecycleViewDivider(
                GoodsAllCommentsListActivity.this, LinearLayout.VERTICAL, 5, getResources().getColor(R.color.colorTransparent)));
        initListener();
        loadData(1);
    }

    private void initListener() {
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                refreshlayout.finishRefresh(2000/*,false*/);//传入false表示刷新失败
                loadData(1);
            }
        });
        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshlayout) {
                refreshlayout.finishLoadMore(2000/*,false*/);//传入false表示加载失败
                loadData(++page);
            }
        });
    }

    private void loadData(int page) {
        this.page = page;
        HttpParams params = new HttpParams();
        params.put("goodsId", id);
        params.put("page", page);
        params.put("limit", "10");
        CommonUtil.netRv(new GoodCommentsBean(), commentsListAdapter, dataList, MyUrl.goodsComments, params, page, new NetListCallBack<GoodCommentsBean.DataBean>() {
            @Override
            public void callBack(List<GoodCommentsBean.DataBean> list) {
                dataList = list;
                if (dataList == null || dataList.size() == 0) {
                    tvEmpty.setVisibility(View.VISIBLE);
                } else {
                    tvEmpty.setVisibility(View.GONE);
                }
            }
        });
    }


    @OnClick(R.id.tv_title)
    public void onViewClicked() {
        onBackPressed();
    }
}
